<?php
/**
 * CLogDb class file.
 * 数据库存储日志.
 * 存数据库前，需要建log表，表名默认为log，可以自定义.
 * 自定义表名时，需要将表名做为参数赋给构造方法.
 * 如果修改表的字段，需要修改父类代码.
 * CREATE TABLE logs (
 * 	   log_id       bigint(20) unsigned NOT NULL auto_increment COMMENT 'LOG_ID',
 *     message      text       NOT NULL COMMENT 'LOG的内容',
 *     priority     tinyint(1) NOT NULL default '6' COMMENT 'LOG类型',
 *     priorityName enum('EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG') NOT NULL COMMENT 'LOG类型名称',
 *     dt_create    datetime   NOT NULL COMMENT '创建LOG的时间',
 *     PRIMARY KEY  (log_id)
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 * @author Huan Song <iphper@yeah.net>
 * @version $Id: CLogDb.php 1 2010-06-22 09:31:00Z huan.song $
 * @package system.log
 * @since 2.0
 */
class CLogDb extends CLog
{
	/**
	 * @var Database 数据库操作类.
	 */
    private $_db;
    
    /**
     * @var string 日志表名.
     */
    private $_table;
    
    public function __construct($table = 'logs')
    {
        $this->_db = new CDbDriver();
        $this->_db->active = true;
        $this->_table = $table;
    }
	
    /**
     * 关闭数据库.
     * @return void.
     */
    public function shutdown()
    {
        $this->_db = null;
    }
	
    /**
     * 向数据中写一条日志.
     * @param  array  $event  event data.
     * @return void.
     */
    protected function _write($event)
    {
    	$sql = 'INSERT INTO ' . $this->_table . ' (message, priority, priorityName, dt_create) VALUES ';
    	$sql .= "('{$event['message']}', '{$event['priority']}', '{$event['priorityName']}', '{$event['dt_create']}')";
        if (!$this->_db->pdo->exec($sql))
        	throw new CLogException('CLogDb:_write Unable to insert to db');
    }
}
